package MainTest;

/**
 * Created by xuyi on 2017/2/12.
 */
public class BinaryTree {

    public Integer data;

    public BinaryTree leftChild;

    public BinaryTree rightChile;

    public BinaryTree() {
    }
    public BinaryTree(int data) {
        this.data = data;
    }

    public void insert(BinaryTree root, int data) {
        if (root == null) {
            return;
        }
        if (root.data == null) {
            root.data = data;
            return;
        }
        if (root.data > data) { //
            if (root.leftChild != null) {
                this.insert(root.leftChild, data);
            } else {
                root.leftChild = new BinaryTree(data);
            }
        } else {
            if (root.rightChile != null) {
                this.insert(root.rightChile, data);
            } else {
                root.rightChile = new BinaryTree(data);
            }
        }
    }

    public static StringBuilder sb = new StringBuilder("");
    public void preOrder(BinaryTree binaryTree) {
        if (binaryTree != null) {
            sb.append(binaryTree.data).append(",");
            this.preOrder(binaryTree.leftChild);
            this.preOrder(binaryTree.rightChile);
        }
    }

    public static void main(String[] args) {

        int[] arr = {12,76,35,22,16,48,90,46,9,40};

        BinaryTree root = new BinaryTree();

        for (int i = 0; i < arr.length; i++) {
            root.insert(root, arr[i]);
        }
        root.preOrder(root);
        System.out.println(sb.toString());
    }





}
